package com.zs.leymovie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.zs.leymovie.MainActivity;
import com.zs.leymovie.R;
import com.zs.leymovie.adapter.BgFragmentAdapter;
import com.zs.leymovie.fragment.WelcomeFragment;
import com.zs.leymovie.localcity.ActivitySelectCity;
import com.zs.leymovie.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;


public class WelcomeActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private Fragment[] fgs;
    private Handler handler;
    private int currentItem = 0;
    private int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        /**全屏设置，隐藏窗口所有装饰**/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        if (Utils.isFirst(this)) {
            welcome();
        } else {
            Intent intent = new Intent(this, ActivitySelectCity.class);
            startActivityForResult(intent, 99);
        }
    }

    private void welcome() {
        viewPager = (ViewPager) this.findViewById(R.id.bg_welcome_viewpager_container);
        initDataFg();
        BgFragmentAdapter bfa = new BgFragmentAdapter(getSupportFragmentManager(), fgs);
        viewPager.setAdapter(bfa);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg != null) {
                    int currentPager = msg.arg1;
                    currentItem++;
                    viewPager.setCurrentItem(currentPager % fgs.length);
                }
            }
        };
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = Message.obtain();
                if (currentItem < 4) {
                    msg.arg1 = currentItem;
                    handler.sendMessage(msg);
                }
            }
        };
        timer.schedule(timerTask, 0, 2 * 1000);
    }

    private void initDataFg() {
        fgs = new Fragment[4];
        WelcomeFragment bgf1 = new WelcomeFragment(1);
        WelcomeFragment bgf2 = new WelcomeFragment(2);
        WelcomeFragment bgf3 = new WelcomeFragment(3);
        WelcomeFragment bgf4 = new WelcomeFragment(4);
        fgs[0] = bgf1;
        fgs[1] = bgf2;
        fgs[2] = bgf3;
        fgs[3] = bgf4;
    }

    @Override
    protected void onResume() {
        super.onResume();
        flag++;
        if (flag >= 3) {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String city_id = data.getStringExtra("cityid");
        String lngCityName = data.getStringExtra("lngCityName");
        Intent startMainActivity = new Intent(this, MainActivity.class);
        startMainActivity.putExtra("cityid",city_id);
        startMainActivity.putExtra("lngCityName",lngCityName);
        startActivity(startMainActivity);
    }
}
